var ahAppBridge = {
    version : '2.1.1',
    base :{
      toast: function(opt, c) {
        _mapping(opt, {
          _: 'content%s'
        });
        if(!__isString(opt.content)) {
          opt.content = _toType('%s', opt.content)
        }
        ahAppBridge.call("base.toast", opt, c)
      },

      statusBar: function(opt) {
        _mapping(opt, {
          _: 'color%c'
        });
        ahAppBridge.call("base.statusBar", opt)
      },

      menu: function(opt) {
        if(__isString(opt._)) {
          opt.title = opt._;
          delete opt._
        }
        if(__isArray(opt._)) {
          opt.items = opt._;
          delete opt._
        }
        _mapping(opt, {
          items: 'menus',
          type: 'iconType',
          badge: 'redDot%s'
        });
        if(!__isUndefined(opt.icon)) {
          opt.icon = _toType('%b', opt.icon)
        }
        if(__isObject(_CACHE.setOptionButton)) {
          _CACHE.setOptionButton.menus = []
        } else {
          _CACHE.setOptionButton = {
            menus: []
          }
        }
        if(__isArray(opt.menus)) {
          var menus = opt.menus;
          opt.menus = [];
          menus.forEach(function(item, i) {
            item = _mapping(__extend({},
              item), {
              type: 'icontype',
              badge: 'redDot%s'
            }, {
              badge: __isUndefined(item.badge) ? '-1' : item.badge
            });
            if(!__isUndefined(item.icon)) {
              item.icon = _toType('%b', item.icon)
            }
            opt.menus.unshift(item);
            _CACHE.setOptionButton.menus[menus.length - 1 - i] = i
          });
          if(opt.menus.length > 0 && __isUndefined(opt.override)) {
            opt.override = true
          }
        }
        if(__isFunction(_CACHE.setOptionButton.onEvent)) {
          AP.off('optionMenu', _CACHE.setOptionButton.onEvent)
        }
        ahAppBridge.call("base.menu", {
            'title': opt.title,
            'item': opt.menus,
            'msg': ''
          },
          opt.onClick)
      },
      communicate:function(opt, c){
        var funcName = "base.communicate";
        _mapping(opt, {
          type: 'type%s',
          num : 'num%s',
          content : 'content%s'
        });
        if(__isUndefined(opt.type)){
          ahAppBridge.call("base.toast", "未定义的操作")
          return
        }
        if(__isUndefined(opt.num)){
          opt.num = "110";
        }
        if(__isUndefined(opt.content)){
          opt.content = "";
        }
        ahAppBridge.call(funcName, opt, c);
      },

      longPress:function(opt, c){
        ahAppBridge.call("base.longPress", opt, c);
      },

      //获取硬件信息
      deviceInfo: function(c){
        ahAppBridge.call("base.deviceInfo",{},c);
      },

      //获取应用信息
      appInfo: function(c){
        ahAppBridge.call("base.appInfo",{},c);
      },

      //退出应用
      exit: function(){
        ahAppBridge.call("base.exit", {});
      },

      //下载文件
      download:function(opt){
         ahAppBridge.call("base.download", opt);
       },

       //设置临时数据
      setSession:function(opt, c) {
         opt = _fixOptData(opt);
         if (!__isObject(opt.data)) {
           opt.data = {
             data: opt.data
           };
         }
         __forEach(opt.data, function (key, value) {
           opt.data[key] = JSON.stringify(value);
         });
         ahAppBridge.call("base.setSession", opt, c);
       },
       //获取临时数据
      getSession:function(opt, c){
       var _opt = {_:opt}
       if (__isString(_opt._)) {
         _opt.keys = [_opt._];
       }
       if (__isArray(_opt._)) {
         _opt.keys = _opt._;
       }
       delete _opt._;
       ahAppBridge.call("base.getSession", _opt, a);
       a: function a(res) {
           res = __parseJSON(res);
           res.data = __parseJSON(res.data);
           __forEach(res.data, function (key, value) {
                     res.data[key] = __parseJSON(value);
           if(__isFunction(c)) {
             c(res)
           }
         });
       }
     }
		},
		toggle :{
		  func: function(opt, c){
        var prefix = "toggle";
        _mapping(opt, {
          type: 'type%s',
          tag : 'toggle%s'
        });
        if(__isUndefined(opt.toggle)) {
          opt.toggle = '0';
        }
        if(__isUndefined(opt.type)){
          this.call("toast","未定义的函数")
          return;
        }
        var func = opt.type
        ahAppBridge.call("toggle."+func, opt, c);
      },
		},
		titleBar :{
		  setTitleBar: function(opt, c) {
        var st = 'title';
        var stc = 'color';
        var _opt = {};
        var res = {};
        _opt[st] = {};
        _opt[stc] = {};
        // 映射不同 JSAPI 的入参c
        _opt[st] = _mapping(_opt[st], {
            _: 'title',
            //接口可直接传入一个字符串（opt.title）
            title: 'title%s',
            image: 'image%b' //处理 image 为 base64 的情况，为 native 移除格式头
          },
          opt);
        _opt[stc] = _mapping(_opt[stc], {
            backgroundColor: 'color%c',
            reset: 'reset'
          },
          opt);
        //_opt映射完成
        //setTitle
        if(!__isEmptyObject(_opt[st])) {
          ahAppBridge.call('titleBar.'+st, _opt[st],
            function(result) {
              res = __extend(result, res);
              c(res);
            });
        }
        //setTitleColor
        if(!__isEmptyObject(_opt[stc])) {
          ahAppBridge.call('titleBar.'+stc, _opt[stc],
            function(result) {
              res = __extend(result, res);
              c(res);
            });
        } else {
          //setTitle 和 setBarBottomLineColor 本身没有回调
          //为保持接口一致性要模拟一个异步回调
          _fakeCallBack(c, res);
        }
      },
		  setOptionButton: function(opt) {
        if(__isString(opt._)) {
          opt.title = opt._;
          delete opt._;
        }
        if(__isString(opt.type)) {
          opt.buttonType = opt.type;
        } else {
          opt.buttonType = 'right';
        }
        if(__isArray(opt._)) {
          opt.items = opt._;
          delete opt._;
        }
        _mapping(opt, {
          items: 'menus',
          type: 'iconType',
          badge: 'redDot%s'
        });
        if(!__isUndefined(opt.icon)) {
          opt.icon = _toType('%b', opt.icon);
        }
        //optionMenu事件只监听一次，防止多次回调
        if(__isObject(_CACHE.setOptionButton)) {
          _CACHE.setOptionButton.menus = [];
        } else {
          _CACHE.setOptionButton = {
            menus: []
          };
        }
        if(__isArray(opt.menus)) {
          var menus = opt.menus;
          //需要复制一份，不能在原数组上修改，会破坏用户数据
          opt.menus = [];
          menus.forEach(function(item, i) {
            item = _mapping(__extend({}, item), {
              type: 'icontype',
              tag: 'tag%s',
              badge: 'redDot%s'
            }, {
              badge: __isUndefined(item.badge) ? '-1' : item.badge,
              tag: i
            });
            if(!__isUndefined(item.icon)) {
              item.icon = _toType('%b', item.icon);
            }
            if (!__isUndefined(item.title)) {
              item.icontype = "title";
              item.iconcontent = item.title;
            }else if (!__isUndefined(item.icon)) {
              item.icontype = "icon";
              item.iconcontent = item.icon;
            }else if (!__isUndefined(item.type)) {
              item.icontype = "type";
              item.iconcontent = item.type
            }else{
              this.call("toast", "title,icon,type 至少有一个")
              return;
            }
            delete item.icon;
            delete item.title;
            delete item.type;
            opt.menus.unshift(item);
            _CACHE.setOptionButton.menus[menus.length - 1 - i] = i;
          });
          if(opt.menus.length > 0 && __isUndefined(opt.override)) {
            opt.override = true;
          }
        }
        //每次 setOptionMenu 要注册新的事件 TODO 这里是否有存在的必要？
        if(__isFunction(_CACHE.setOptionButton.onEvent)) {
          AP.off('optionMenu', _CACHE.setOptionButton.onEvent);
        }
        ahAppBridge.call("titleBar.buttons", {
          'item': opt.menus,
          'msg': '',
          'buttonType': opt.buttonType
        }, opt.onClick);
      },
      showPopMenu: function(opt) {
        //其他属性映射
        _mapping(opt, {
          _: 'items',
          items: 'menus'
        });
        //popMenuClick事件只监听一次，防止多次回调
        if(__isObject(_CACHE.showPopMenu)) {
          _CACHE.showPopMenu.menus = {};
        } else {
          _CACHE.showPopMenu = {
            menus: {}
          };
        }
        if(__isArray(opt.menus)) {
          var menus = opt.menus;
          //需要复制一份，不能在原数组上修改，会破坏用户数据
          opt.menus = [];
          menus.forEach(function(item, i) {
            //支持菜单直接是个字符串数组
            if(__isString(item)) {
              item = {
                title: item
              };
            }
            item = _mapping(__extend({},
              item), {
              title: 'name%s',
              tag: 'tag%s',
              badge: 'redDot%s'
            }, { //默认设置，index和隐藏小红点
              tag: i,
              title: item.title,
              badge: __isUndefined(item.badge) ? '-1' : item.badge
            });
            if(!__isUndefined(item.icon)) {
              item.icon = _toType('%b', item.icon);
            }
            opt.menus.push(item);
            _CACHE.showPopMenu.menus[item.name] = i;
          });
        }
        ahAppBridge.call("titleBar.menu", {
            'item': opt.menus,
            'msg': ''
          },
          opt.onClick)
      }
		},
		third:{
      pushAlias: function(opt, c) {
        _mapping(opt, {
//          _: 'tags%s',
          _: 'alias%s'
        });
        if(!__isString(opt.alias)) {
          opt.alias = _toType('%s', opt.alias)
        }
//        if(!__isUndefined(opt.tags)) {
//          opt.tags = _toType('%s', opt.tags)
//        }
        ahAppBridge.call("third.pushAlias", opt, c)
      },
      pushTags: function(opt, c) {
        _mapping(opt, {
          _: 'tags%s',
//          _: 'alias%s'
        });
//        if(!__isString(opt.alias)) {
//          opt.alias = _toType('%s', opt.alias)
//        }
        if(!__isUndefined(opt.tags)) {
          opt.tags = _toType('%s', opt.tags)
        }
        ahAppBridge.call("third.pushTags", opt, c)
      },
      pushClean: function(c){
        ahAppBridge.call("third.pushClean",{},c);
      },
      location: function(opt) {
        _mapping(opt, {
          accuracy: 'horizontalAccuracy',
          type: 'requestType'
        });
        if(__isUndefined(opt.requestType)) {
          opt.requestType = 2
        }
        if(__isAndroid()) {
          if(__isUndefined(opt.isHighAccuracy)) {
            opt.isHighAccuracy = true
          }
          if(__isUndefined(opt.isNeedSpeed)) {
            opt.isNeedSpeed = true
          }
        }
        ahAppBridge.call("third.location", opt, opt)
      },
      chooseImage: function(opt, c) {
        _mapping(opt, {
          _: 'count%d'
        });
        if(__isUndefined(opt.count)) {
          opt.count = 1
        }
        if(__isString(opt.sourceType)) {
          opt.sourceType = [opt.sourceType]
        }
        a: function a(res) {
          _mapping(res, {
            errorCode: 'error',
            errorDesc: 'errorMessage',
            localIds: 'apFilePaths',
            tempFilePaths: 'apFilePaths'
          });
          delete res.scene;
          delete res.localIds;
          delete res.tempFilePaths;
          if(__isString(res.apFilePaths)) {
            res.apFilePaths = __parseJSON(res.apFilePaths)
          }
          res = __parseJSON(res);
          if(__isFunction(c)) {
            c(res)
          }
        }
        ahAppBridge.call("third.chooseImage", opt, a)
      },
      //分享
      share: function(opt,c) {
      if(__isUndefined(opt.title)){
        opt.title = "title"
      }
      if(__isUndefined(opt.content)){
        opt.content = "content"
      }
      if(__isUndefined(opt.url)){
        opt.url = "title"
      }
      if(__isUndefined(opt.image)){
        opt.image = "title"
      }
      if(!__isString(opt.platform)){
        opt.platform = null
      }
        ahAppBridge.call("third.share",opt, c)
      },
      //scan
      scan: function(opt, c) {
        if(__isUndefined(opt.title)){
          opt.title = "title"
        }
        ahAppBridge.call("third.scan",opt, c)
      },
      thirdPartyWeb: function(opt, c) {
        ahAppBridge.call("third.thirdPartyWeb", opt, c);
      },
      clickCustom:function(opt, c){
        var _opt = opt;
        if(__isString(opt)){
          _opt = {index:opt};
        }
        if(__isUndefined(_opt.index))
          _opt.index = "1";
        ahAppBridge.call("third.clickCustom", _opt, c);
      }
		},
    default:this,// for typescript
    cbs:[],
    call: function (method, args, cb) {
        var ret = '';
        if (typeof args == 'function') {
            cb = args;
            args = {};
        }
        var arg={data:args===undefined?null:args}
        if(typeof cb == 'function'){
          var cbName = 'dscb_'+method;
          this.cbs[cbName] = cb;
          arg['_dscbstub'] = cbName;
        }

        arg = JSON.stringify(arg)

        //if in webview that dsBridge provided, call!
        if(window._Bifrost){
           ret=  _Bifrost.call(method, arg)
        }

       return  JSON.parse(ret||'{}').data
    },
    hasNativeMethod: function (name, type) {
        return ahAppBridge.call("_Bifrost_Check.hasNativeMethod", {name: name, type:type||"all"});
    }
};